package com.kxmall.web.controller.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.coupon.domain.KxStoreCouponIssue;
import com.kxmall.coupon.domain.bo.KxStoreCouponIssueBo;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.utils.StringUtils;
import com.kxmall.coupon.domain.vo.KxStoreCouponIssueVo;
import com.kxmall.coupon.mapper.KxStoreCouponIssueMapper;
import com.kxmall.web.controller.coupon.service.IKxStoreCouponIssueService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 优惠券前台领取Service业务层处理
 *
 * @author kxmall
 * @date 2023-02-17
 */
@RequiredArgsConstructor
@Service
public class KxStoreCouponIssueServiceImpl implements IKxStoreCouponIssueService {

    private final KxStoreCouponIssueMapper baseMapper;

    /**
     * 查询优惠券前台领取
     */
    @Override
    public KxStoreCouponIssueVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询优惠券前台领取列表
     */
    @Override
    public TableDataInfo<KxStoreCouponIssueVo> queryPageList(KxStoreCouponIssueBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KxStoreCouponIssue> lqw = buildQueryWrapper(bo);
        Page<KxStoreCouponIssueVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询优惠券前台领取列表
     */
    @Override
    public List<KxStoreCouponIssueVo> queryList(KxStoreCouponIssueBo bo) {
        LambdaQueryWrapper<KxStoreCouponIssue> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KxStoreCouponIssue> buildQueryWrapper(KxStoreCouponIssueBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KxStoreCouponIssue> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCname()), KxStoreCouponIssue::getCname, bo.getCname());
        lqw.eq(bo.getCid() != null, KxStoreCouponIssue::getCid, bo.getCid());
        lqw.eq(bo.getCtype() != null, KxStoreCouponIssue::getCtype, bo.getCtype());
        lqw.eq(bo.getStartTime() != null, KxStoreCouponIssue::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, KxStoreCouponIssue::getEndTime, bo.getEndTime());
        lqw.eq(bo.getTotalCount() != null, KxStoreCouponIssue::getTotalCount, bo.getTotalCount());
        lqw.eq(bo.getRemainCount() != null, KxStoreCouponIssue::getRemainCount, bo.getRemainCount());
        lqw.eq(bo.getIsPermanent() != null, KxStoreCouponIssue::getIsPermanent, bo.getIsPermanent());
        lqw.eq(bo.getStatus() != null, KxStoreCouponIssue::getStatus, bo.getStatus());
        lqw.eq(bo.getIsDel() != null, KxStoreCouponIssue::getIsDel, bo.getIsDel());
        return lqw;
    }

    /**
     * 新增优惠券前台领取
     */
    @Override
    public Boolean insertByBo(KxStoreCouponIssueBo bo) {
        KxStoreCouponIssue add = BeanUtil.toBean(bo, KxStoreCouponIssue.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改优惠券前台领取
     */
    @Override
    public Boolean updateByBo(KxStoreCouponIssueBo bo) {
        KxStoreCouponIssue update = BeanUtil.toBean(bo, KxStoreCouponIssue.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KxStoreCouponIssue entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除优惠券前台领取
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
